package com.zhuhao.design_mode.creational_pattern.builder_pattern;

/**
 * 构建者
 *
 * @Author haozhu7
 * @Date 2025/11/13 10:34
 **/
public class UserBuilder {

    //持有需要创建的对象
    private User user;

    public UserBuilder() {
        this.user = new User();
    }

    public UserBuilder username(String username) {
        this.user.setUsername(username);
        return this;  // 关键：返回this，实现链式调用
    }

    public UserBuilder password(String password) {
        this.user.setPassword(password);
        return this;  // 关键：返回this，实现链式调用
    }

    public UserBuilder nickname(String nickname) {
        this.user.setNickname(nickname);
        return this;  // 关键：返回this，实现链式调用
    }

    public UserBuilder age(int age) {
        this.user.setAge(age);
        return this;
    }

    public UserBuilder email(String email) {
        this.user.setEmail(email);
        return this;
    }

    // 构建最终对象的方法
    public User build() {
        return user;  // 调用User的私有构造器
    }
}